package archimede;

import java.awt.Graphics;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class PannelloPiantina extends JPanel {

  private int tab;
  private Piantina tabbed;
  private StartingWindow st;
  private BarraPulsanti barra;

  protected void paintComponent(Graphics g) {
    setOpaque(false);
    g.drawImage(
        (new ImageIcon(ClassLoader.getSystemResource("sfondo.jpg"))).getImage(),
        0, 0, null);
    super.paintComponent(g);
  }

  public PannelloPiantina(Piantina tabbed, int count, StartingWindow st) {
    super();
    this.st = st;
    this.tabbed = tabbed;
    tab = count;
    setLayout(new MigLayout());
    barra = new BarraPulsanti(this, st);
    add(barra, "dock north");
  }

  public void refresh() {
    paintAll(getGraphics());
  }

  public void setTitle(String title) {
    tabbed.setTitleAt(tab, title);
  }

  public void salva() {
    // System.out.println("tab "+tab);
    Salva s = new Salva(barra.getpanel(), st.getProject());
    // System.out.println("dir "+st.getCount());
    s.salvaImmaginePNG("piantina" + tab + ".png");
  }

  public String getTitle() {
    return tabbed.getTitleAt(tab);
  }

  public void setPiantina(String path) {
    Properties pc = System.getProperties();
    String homePath = pc.getProperty("user.home");
    String directoryProject = homePath + pc.getProperty("file.separator")
        + "ArchimedeSuper" + pc.getProperty("file.separator") + st.getProject()
        + pc.getProperty("file.separator");

    barra.setElement(directoryProject + "piantina" + tab + ".png");
  }

  public Document pdf(Document doc) {
    doc.newPage();
    Chunk underline = new Chunk("5. Pianta dell'edificio " + getTitle(),
        FontFactory.getFont("Times", 15.0F, 1));
    underline.setUnderline(1.2F, -2.0F);
    Anchor anchorTarget = new Anchor(underline);
    anchorTarget.setName("pianta");
    Paragraph targetParagraph = new Paragraph();
    targetParagraph.add(anchorTarget);
    try {
      targetParagraph.setSpacingAfter(10);
      doc.add(targetParagraph);
    } catch (DocumentException e2) {
      e2.printStackTrace();
    }

    Properties pc = System.getProperties();
    String homePath = pc.getProperty("user.home");
    String directoryProject = homePath + pc.getProperty("file.separator")
        + "ArchimedeSuper" + pc.getProperty("file.separator")
        + this.st.getProject() + pc.getProperty("file.separator") + "piantina"
        + this.tab + ".png";

    PdfPTable table = new PdfPTable(6);
    table.setWidthPercentage(100.0F);
    try {
      Image img = Image.getInstance(directoryProject);

      float w = img.getWidth();
      float h = img.getHeight();
      float propozione = -1;
      Rectangle page = doc.getPageSize();

      if (w > page.getWidth() - 100.0F) {
        propozione = ((page.getWidth() - 105) / w) * 100;
      }
      if (h > page.getHeight() - 250.0F) {
        float temp = ((page.getHeight() - 260) / h) * 100;
        propozione = temp < propozione ? temp : propozione;
      }

      if (propozione > 0) {
        System.out.println(propozione);
        img.scalePercent(propozione, propozione);
      }

      // if ((w > page.getWidth() - 100.0F) && (h > page.getHeight() - 250.0F))
      // img.scaleAbsolute(page.getWidth() - 100.0F, page.getHeight() - 250.0F);
      // else if ((w > page.getWidth() - 100.0F)
      // && (h < page.getHeight() - 250.0F))
      // img.scaleAbsolute(page.getWidth() - 100.0F, h);
      // else if ((w < page.getWidth() - 100.0F)
      // && (h > page.getHeight() - 250.0F))
      // img.scaleAbsolute(w, page.getHeight() - 250.0F);

      PdfPCell cos = new PdfPCell(img);
      cos.setHorizontalAlignment(1);
      cos.setVerticalAlignment(5);
      cos.setBorder(0);
      cos.setColspan(6);
      table.addCell(cos);

      cos = new PdfPCell(new Phrase("Legenda piantina", FontFactory.getFont(
          "Times", 12.0F, 1)));
      cos.setHorizontalAlignment(0);
      cos.setVerticalAlignment(5);
      cos.setBorder(0);
      cos.setColspan(6);
      table.addCell(cos);

      com.itextpdf.text.Image legend = com.itextpdf.text.Image
          .getInstance(ClassLoader.getSystemResource("p_legend.png"));
      legend.scalePercent(13, 13);
      cos = new PdfPCell(legend);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setBorder(0);
      table.addCell(cos);

      cos = new PdfPCell(new Phrase("Punto rilievo critico",
          FontFactory.getFont("Times", 10.0F)));
      cos.setHorizontalAlignment(Element.ALIGN_LEFT);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setBorder(0);
      cos.setColspan(2);
      table.addCell(cos);

      legend = com.itextpdf.text.Image.getInstance(ClassLoader
          .getSystemResource("t_legend.png"));
      legend.scalePercent(13, 13);
      cos = new PdfPCell(legend);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setBorder(0);
      table.addCell(cos);

      cos = new PdfPCell(new Phrase("Punto misurazione potenziali elettrici",
          FontFactory.getFont("Times", 10.0F)));
      cos.setHorizontalAlignment(Element.ALIGN_LEFT);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setBorder(0);
      cos.setColspan(2);
      table.addCell(cos);

      legend = com.itextpdf.text.Image.getInstance(ClassLoader
          .getSystemResource("c_legend.png"));
      legend.scalePercent(13, 13);
      cos = new PdfPCell(legend);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setBorder(0);
      table.addCell(cos);

      cos = new PdfPCell(new Phrase("Punto rilievo clima", FontFactory.getFont(
          "Times", 10.0F)));
      cos.setHorizontalAlignment(Element.ALIGN_LEFT);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setBorder(0);
      cos.setColspan(2);
      table.addCell(cos);

      legend = com.itextpdf.text.Image.getInstance(ClassLoader
          .getSystemResource("m_legend.png"));
      legend.scalePercent(13, 13);
      cos = new PdfPCell(legend);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setBorder(0);
      table.addCell(cos);

      cos = new PdfPCell(new Phrase("Prelievo campione misura ponderale",
          FontFactory.getFont("Times", 10.0F)));
      cos.setHorizontalAlignment(Element.ALIGN_LEFT);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setBorder(0);
      cos.setColspan(2);
      table.addCell(cos);
      doc.add(table);
    } catch (BadElementException e) {
      e.printStackTrace();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    }

    return doc;
  }

  public String print() {
    return tabbed.getTitleAt(tab);
  }

  public int getTab() {
    return tab;
  }

}
